<?php
/**
 * Created by PhpStorm.
 * User: david
 * Date: 2018/5/29
 * Time: 9:52
 */

namespace app\admin\controller;


use app\admin\Controller;
use traits\controller\Jump;
use app\common\model\Project as ProjectModel;
use app\common\model\Xmdj as XmdjModel;
use app\common\model\Example as ExampleModel;

class Example extends Controller
{
    use \app\admin\traits\controller\Controller;
    use Jump;

    public function add()
    {
        if ($this->request->isAjax()) {
            $data = [];
            $data['name'] = $this->request->param('name');
            $data['project_id'] = $this->request->param('project_id');
            $data['poster'] = $this->request->param('poster');
            ExampleModel::create($data);
            return ajax_return_adv('添加成功');
        } else {
            $projects = XmdjModel::getAll();
            $this->view->assign('projects', $projects);
            return $this->view->fetch('edit');
        }
    }

    public function edit()
    {
        if ($this->request->isAjax()) {
            $id = $this->request->param('id');
            $data = [];
            $data['name'] = $this->request->param('name');
            $data['poster'] = $this->request->param('poster');
            $data['project_id'] = $this->request->param('project_id');
            ExampleModel::where('id', $id)->update($data);
            return ajax_return_adv('修改成功');
        } else {
            $id = $this->request->param('id');
            $example = ExampleModel::get($id);
            $projects = ProjectModel::getProjects();
            $this->view->assign('example', $example);
            $this->view->assign('projects', $projects);
            return $this->view->fetch('edit');
        }
    }
}